package org.JQbitorrentClinet.impl.V283;

import org.JQbitorrentClinet.QBitorrentConnectionInfo;
import org.JQbitorrentClinet.base.BaseProxy;
import org.JQbitorrentClinet.interfaces.TorrentManageProxy;
import org.JQbitorrentClinet.pojo.torrent.*;
import org.JQbitorrentClinet.request.torrentmanagement.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TorrentManage_V2_8_3 extends BaseProxy implements TorrentManageProxy {
    public TorrentManage_V2_8_3(QBitorrentConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    @Override
    public List<TorrentInfo> getTorrentList() {
        login();
        List result = process(GetTorrentListParam.builder().build());
        if(result == null)
            result = new ArrayList<>();
        return result;
    }

    public TorrentInfo getTorrentInfo(String hash) {
        List<TorrentInfo> torrentList = getTorrentList();
        for (TorrentInfo torrentInfo : torrentList) {
            if (torrentInfo.getHash().equals(hash)) {
                return torrentInfo;
            }
        }
        return null;
    }

    @Override
    public TorrentGenericProperties getTorrentProperties(String hash) {
        login();
        return process(GetTorrentGenericPropertiesParam.builder().hash(hash).build());
    }

    @Override
    public List<TrackerInfo> getTorrentTrackers(String hash) {
        login();
        return process(GetTorrentTrackersParam.builder().hash(hash).build());
    }

    @Override
    public List<TorrentWebSeed> getTorrentWebSeeds(String hash) {
        login();
        List result = process(GetTorrentWebSeedsParam.builder().hash(hash).build());
        if(result == null)
            result = new ArrayList<>();
        return result;
    }

    @Override
    public List<TorrentContent> getTorrentContents(String hash, String[] indexes) {
        login();
        return process(GetTorrentContentsParam.builder().hash(hash).indexes(indexes).build());
    }

    @Override
    public List<Integer> getTorrentPiecesStates(String hash) {
        login();
        return process(GetTorrentPiecesStatesParam.builder().hash(hash).build());
    }

    @Override
    public List<String> getTorrentPiecesHashes(String hash) {
        login();
        return process(GetTorrentPiecesHashesParam.builder().hash(hash).build());
    }

    @Override
    public boolean pauseTorrent(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(PauseTorrentsParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean resumeTorrent(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(ResumeTorrentsParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean deleteTorrent(List<String> hashes, boolean deleteFiles) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(DeleteTorrentsParam.builder().hashes(array).deleteFiles(deleteFiles).build());
        return true;
    }

    @Override
    public boolean recheckTorrent(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(RecheckTorrentsParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean reannounceTorrent(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(ReannounceTorrentsParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean addNewTorrent(AddNewTorrentParam param) {
        login();
        process(param);
        return true;
    }

    @Override
    public boolean addTrackersToTorrent(String hash, List<String> trackers) {
        login();
        String[] array = new String[trackers.size()];
        trackers.toArray(array);
        process(AddTrackersToTorrentParam.builder().hash(hash).urls(array).build());
        return true;
    }

    @Override
    public boolean editTrackersInTorrent(EditTrackersParams params) {
        login();
        process(params);
        return true;
    }

    @Override
    public boolean removeTrackersFromTorrent(String hash, List<String> trackers) {
        login();
        String[] array = new String[trackers.size()];
        trackers.toArray(array);
        process(RemoveTrackersParam.builder().hash(hash).urls(array).build());
        return true;
    }

    @Override
    public boolean addPeersToTorrent(List<String> hashes, List<String> peers) {
        login();
        String[] arrayHashes = new String[hashes.size()];
        hashes.toArray(arrayHashes);
        String[] arrayPeers = new String[peers.size()];
        peers.toArray(arrayPeers);
        process(AddPeersParam.builder().hashes(arrayHashes).peers(arrayPeers).build());
        return true;
    }

    @Override
    public boolean increaseTorrentPriority(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(IncreaseTorrentPriorityParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean decreaseTorrentPriority(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(DecreaseTorrentPriorityParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean maxTorrentPriority(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(MaximalTorrentPriorityParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean minTorrentPriority(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(MinimalTorrentPriorityParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean setTorrentFilePriority(SetFilePriorityParam param) {
        login();
        return process(param);
    }

    @Override
    public Map<String, Long> getTorrentDownloadLimit(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        return process(GetTorrentDownloadLimitParam.builder().hashes(array).build());
    }

    @Override
    public boolean setTorrentDownloadLimit(List<String> hashes, Long limit) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetTorrentDownloadLimitParam.builder().hashes(array).limit(limit).build());
        return true;
    }

    @Override
    public boolean setTorrentShareLimit(List<String> hashes,Float ratioLimit,Long seedingTimeLimit) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetTorrentShareLimitParam.builder().hashes(array).seedingTimeLimit(seedingTimeLimit).ratioLimit(ratioLimit).build());
        return true;
    }

    @Override
    public Map<String, Long> getTorrentUploadLimit(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        return process(GetTorrentUploadLimitParam.builder().hashes(array).build());
    }

    @Override
    public boolean setTorrentUploadLimit(List<String> hashes, Long limit) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetTorrentUploadLimitParam.builder().hashes(array).limit(limit).build());
        return true;
    }

    @Override
    public boolean setTorrentLocation(List<String> hashes, String location) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetTorrentLocationParam.builder().hashes(array).location(location).build());
        return true;
    }

    @Override
    public boolean setTorrentName(String hash, String name) {
        login();
        process(SetTorrentNameParam.builder().hash(hash).name(name).build());
        return true;
    }

    @Override
    public boolean setTorrentCategory(List<String> hashes, String category) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetTorrentCategoryParam.builder().hashes(array).category(category).build());
        return true;
    }

    @Override
    public  Map<String, Category> getAllCategories() {
        login();
        return process(GetAllCategoriesParam.builder().build());
    }

    @Override
    public boolean addCategory(Category category) {
        login();
        process(AddNewCategoryParam.builder().category(category.getName()).savePath(category.getSavePath()).build());
        return true;
    }

    @Override
    public boolean editCategory(Category category) {
        login();
        process(EditCategoryParam.builder().category(category.getName()).savePath(category.getSavePath()).build());
        return true;
    }

    @Override
    public boolean removeCategories(List<String> categories) {
        login();
        String[] array = new String[categories.size()];
        categories.toArray(array);
        process(RemoveCategoryParam.builder().categories(array).build());
        return true;
    }

    @Override
    public boolean addTagsToTorrent(List<String> hashes, List<String> tags) {
        login();
        String[] arrayHashes = new String[hashes.size()];
        hashes.toArray(arrayHashes);
        String[] arrayTags = new String[tags.size()];
        tags.toArray(arrayTags);
        process(AddTagsParam.builder().hashes(arrayHashes).tags(arrayTags).build());
        return true;
    }

    @Override
    public boolean removeTagsFromTorrent(List<String> hashes, List<String> tags) {
        login();
        String[] arrayHashes = new String[hashes.size()];
        hashes.toArray(arrayHashes);
        String[] arrayTags = new String[tags.size()];
        tags.toArray(arrayTags);
        process(RemoveTorrentTags.builder().hashes(arrayHashes).tags(arrayTags).build());
        return true;
    }

    @Override
    public List<String> getTags() {
        login();
        List<String> result = process(GetAllTagsParam.builder().build());
        if(result == null)
            result = new ArrayList<>();

        return result;
    }

    @Override
    public boolean createTags(List<String> tags) {
        login();
        String[] array = new String[tags.size()];
        tags.toArray(array);
        process(CreateTagsParam.builder().tags(array).build());
        return true;
    }

    @Override
    public boolean deleteTags(List<String> tags) {
        login();
        String[] array = new String[tags.size()];
        tags.toArray(array);
        process(DeleteTagsParam.builder().tags(array).build());
        return true;
    }

    @Override
    public boolean setAutoManagement(List<String> hashes, boolean enable) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetAutomaticTorrentManagementParam.builder().hashes(array).enable(enable).build());
        return true;
    }

    @Override
    public boolean ToggleSequentialDownload(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(ToggleSequentialDownloadParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean setFirstLastPiecePriority(List<String> hashes) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetFirstLastPiecePriorityParam.builder().hashes(array).build());
        return true;
    }

    @Override
    public boolean setForceStart(List<String> hashes,boolean enable) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetForceStartParam.builder().hashes(array).value(enable).build());
        return true;
    }

    @Override
    public boolean setSuperSeeding(List<String> hashes, boolean enable) {
        login();
        String[] array = new String[hashes.size()];
        hashes.toArray(array);
        process(SetSuperSeedingParam.builder().hashes(array).value(enable).build());
        return true;
    }

    @Override
    public boolean renameFiles(String hash,String oldPath,String newPath) {
        login();
        process(RenameFileParam.builder().hash(hash).oldPath(oldPath).newPath(newPath).build());
        return true;
    }

    @Override
    public boolean renameFolder(String hash, String oldFolder, String newFolder) {
        login();
        process(RenameFolderParam.builder().hash(hash).oldPath(oldFolder).newPath(newFolder).build());
        return true;
    }
}
